package cn.catold.sspay;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by catold on 2018/2/1.
 */
@RestController("return")
public class ReturnController {

    @RequestMapping(value = "ali")
    public String ali(HttpServletRequest request) {
        String result = "";
        //遍历所有字段代码样例
        Enumeration<String> enu = request.getParameterNames();
        while(enu.hasMoreElements()) {
            String key = enu.nextElement();
            System.out.println("key:" + key + ";value:" + request.getParameter(key));
        }

	    /*业务处理代码样例*/
        Object tradeStatus = request.getParameter("trade_status");

        if (tradeStatus != null) {
            if (tradeStatus.toString().equals("TRADE_SUCCESS") || "TRADE_FINISH".equals(tradeStatus.toString())) {
                //成功逻辑
                result = "<h3>支付宝网页支付成功，商户应自行实现成功逻辑！</h3>";
            } else {
                result = "<h3>支付宝网页支付未成功，商户应自行实现失败逻辑！</h3>";
            }
        } else {
            result = "<h3>支付宝网页支付未收到同步通知，商户应自行实现逻辑！</h3>";
        }
        return result;
    }


    @RequestMapping(value = "wx")
    public String wx(HttpServletRequest request) {
        String result = "";
        //遍历所有字段代码样例
        Enumeration<String> enu = request.getParameterNames();
        while(enu.hasMoreElements()) {
            String key = enu.nextElement();
            System.out.println("key:" + key + ";value:" + request.getParameter(key));
        }

	    /*业务处理代码样例*/
        Object tradeStatus = request.getParameter("trade_status");

        if (tradeStatus != null) {
            if (tradeStatus.toString().equals("TRADE_SUCCESS") || "TRADE_FINISH".equals(tradeStatus.toString())) {
                //成功逻辑
                result = "<h3>支付宝网页支付成功，商户应自行实现成功逻辑！</h3>";
            } else {
                result = "<h3>支付宝网页支付未成功，商户应自行实现失败逻辑！</h3>";
            }
        } else {
            result = "<h3>支付宝网页支付未收到同步通知，商户应自行实现逻辑！</h3>";
        }
        return result;
    }


}
